package com.lzx.iteam.net;

import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.PayToolsGroupData;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by xuepengju on 14-3-20.
 */
public class PayToolsGroupMsg extends CcMsgStructure {

    private Message mCallback;

    public PayToolsGroupMsg(Message callback){
        mCallback = callback;
    }

    @Override
    public void onReceive(JSONObject data) {

        ArrayList<PayToolsGroupData> mList = new ArrayList<PayToolsGroupData>();

        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONArray groups = jsonObject.getJSONArray("groups");
            for (int i = 0; i < groups.length(); i++) {
                JSONObject group = groups.getJSONObject(i);
                PayToolsGroupData payToolsGroupData = new PayToolsGroupData();
                payToolsGroupData.setGroup_img(group.getString("group_img"));
                payToolsGroupData.setGroup_name(group.getString("group_name"));
                payToolsGroupData.setGroup_id(group.getString("group_id"));
                payToolsGroupData.setApproval_admin_limit(group.getInt("approval_admin_limit"));
                payToolsGroupData.setAttendance_count_limit(group.getInt("attendance_count_limit"));
                payToolsGroupData.setMember_limit(group.getInt("member_limit"));
                payToolsGroupData.setAttendance_member_limit(group.getInt("attendance_member_limit"));
                payToolsGroupData.setCrm_member_limit(group.getInt("crm_member_limit"));
                payToolsGroupData.setWeekly_templ_limit(group.getInt("weekly_templ_limit"));
                payToolsGroupData.setWeekly_member_limit(group.getInt("weekly_member_limit"));
                payToolsGroupData.setCrm_status(group.getInt("crm_status"));
                payToolsGroupData.setLegwork_status(group.getInt("legwork_status"));
                mList.add(payToolsGroupData);
            }
            if(data != null){
                if(mCallback != null){
                    mCallback.arg1 = 0;
                    mCallback.obj = mList;
                    mCallback.sendToTarget();
                }
            }
        } catch (JSONException e) {
            Log.e("PayToolsGroupMsg",e.toString());
        }

    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mCallback != null){
            mCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mCallback.obj = errorMsg;
            }else{
                mCallback.obj = Constants.NETWORK_WRROR;
            }
            mCallback.sendToTarget();
        }
    }
}
